package com.iknow.ui.model;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.RemoteException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.iknow.IKnow;
import com.iknow.image.ImageCache;
import com.iknow.image.ProfileImageCacheCallback;
import com.iknow.image.ProfileImageCacheManager;
import com.iknow.util.Loger;
import com.iknow.xmpp.service.Contact;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.aidl.IRoster;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class FriendAdapter extends BaseAdapter
{
  private final String TAG = "FriendAdapter";
  private final ComparatorContactListByStatusAndName<Friend> mComparator;
  private ArrayList<Friend> mFriendList;
  private LayoutInflater mInflater = null;
  private boolean mIsOnline;
  private ProfileImageCacheManager mProfileImageCacheManager;
  private IRoster mRoster;
  private boolean mScrolling;
  private boolean mShowLbs = false;
  private boolean mShowTags = false;

  public FriendAdapter(Context paramContext, LayoutInflater paramLayoutInflater)
  {
    this.mIsOnline = false;
    this.mComparator = new ComparatorContactListByStatusAndName(this);
    this.mFriendList = new ArrayList();
    this.mProfileImageCacheManager = new ProfileImageCacheManager();
    this.mInflater = paramLayoutInflater;
  }

  private void fillDataToView(int paramInt, View paramView)
  {
    Friend localFriend = getItem(paramInt);
    ViewHoler localViewHoler = (ViewHoler)paramView.getTag();
    localViewHoler.mUserName.setText(localFriend.getName());
    if (localFriend.getImageUrl() != null)
    {
      Bitmap localBitmap = localViewHoler.mUserHead.getDrawingCache();
      if ((localBitmap != null) && (!(localBitmap.equals(ImageCache.mDefaultProfileBitmap))))
      {
        if (!(localBitmap.isRecycled()))
        {
          Loger.i("FriendAdapter", "recycle--" + localBitmap.toString());
          localBitmap.recycle();
        }
        localViewHoler.mUserHead.setDrawingCacheEnabled(false);
      }
      if (!(this.mScrolling))
      {
        IKnow.mProfileImageCacheManager.displayProfileBimap(localViewHoler.mUserHead, localFriend.getImageUrl());
        if (this.mRoster == null)
          break label263;
      }
    }
    try
    {
      while (true)
      {
        while (true)
        {
          Contact localContact = this.mRoster.getContact(localFriend.getID());
          if (localContact != null)
          {
            if (localContact.getStatus() != 500)
              break label240;
            localViewHoler.mStatus.setBackgroundResource(2130837758);
          }
          if (!(this.mShowTags))
            break label283;
          localViewHoler.mSignature.setText(localFriend.getTags());
          if (this.mShowLbs)
            break label298;
          return;
          localViewHoler.mUserHead.setDrawingCacheEnabled(true);
          localViewHoler.mUserHead.setImageBitmap(ImageCache.mDefaultProfileBitmap);
        }
        localViewHoler.mUserHead.setImageResource(2130837551);
      }
      label263: label283: label298: label240: localViewHoler.mStatus.setBackgroundResource(2130837761);
    }
    catch (RemoteException localRemoteException)
    {
      float f1;
      DecimalFormat localDecimalFormat;
      String str;
      while (true)
      {
        while (true)
        {
          while (true)
          {
            do
              while (true)
                localRemoteException.printStackTrace();
            while (!(this.mIsOnline));
            localViewHoler.mStatus.setBackgroundResource(2130837758);
          }
          localViewHoler.mSignature.setText(localFriend.getSignature());
        }
        f1 = localFriend.getDistance();
        localDecimalFormat = new DecimalFormat("0.0");
        if (f1 != 0D)
          break;
        localViewHoler.mDistance.setText("<5米");
      }
      if (f1 > 1000.0F)
      {
        float f2 = f1 / 1000.0F;
        Object[] arrayOfObject2 = new Object[1];
        arrayOfObject2[0] = localDecimalFormat.format(f2);
        str = String.format("%s千米", arrayOfObject2);
      }
      while (true)
      {
        while (true)
          localViewHoler.mDistance.setText(str);
        Object[] arrayOfObject1 = new Object[1];
        arrayOfObject1[0] = localDecimalFormat.format(f1);
        str = String.format("%s米", arrayOfObject1);
      }
    }
  }

  private View newView()
  {
    ViewHoler localViewHoler = new ViewHoler(this, null);
    View localView = this.mInflater.inflate(2130903060, null);
    localViewHoler.mUserHead = ((ImageView)localView.findViewById(2131296338));
    localViewHoler.mStatus = ((FrameLayout)localView.findViewById(2131296306));
    localViewHoler.mUserName = ((TextView)localView.findViewById(2131296287));
    localViewHoler.mSignature = ((TextView)localView.findViewById(2131296330));
    localViewHoler.mDistance = ((TextView)localView.findViewById(2131296339));
    localView.setTag(localViewHoler);
    return localView;
  }

  public void addFriend(Friend paramFriend)
  {
    this.mFriendList.add(paramFriend);
  }

  public void clearFriend()
  {
    this.mFriendList.clear();
  }

  public int getCount()
  {
    return this.mFriendList.size();
  }

  public Friend getItem(int paramInt)
  {
    Friend localFriend;
    if ((paramInt < 0) || (paramInt >= this.mFriendList.size()))
      localFriend = null;
    while (true)
    {
      return localFriend;
      localFriend = (Friend)this.mFriendList.get(paramInt);
    }
  }

  public long getItemId(int paramInt)
  {
    return paramInt;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    if (paramView == null)
      paramView = newView();
    fillDataToView(paramInt, paramView);
    return paramView;
  }

  public void isShowFriendTags(boolean paramBoolean)
  {
    this.mShowTags = paramBoolean;
  }

  public void removeFriend(String paramString)
  {
    int i = 0;
    while (true)
    {
      if (i >= this.mFriendList.size());
      while (true)
      {
        return;
        if (!(((Friend)this.mFriendList.get(i)).getID().equalsIgnoreCase(paramString)))
          break;
        this.mFriendList.remove(i);
      }
      ++i;
    }
  }

  public void scrollIdle(AbsListView paramAbsListView)
  {
    this.mScrolling = false;
    paramAbsListView.invalidateViews();
  }

  public void setBShowLBS(boolean paramBoolean)
  {
    this.mShowLbs = paramBoolean;
  }

  public void setList(List<Friend> paramList)
  {
    this.mFriendList.addAll(paramList);
  }

  public void setOnline(boolean paramBoolean)
  {
    this.mIsOnline = paramBoolean;
  }

  public void setScrolling(boolean paramBoolean)
  {
    this.mScrolling = paramBoolean;
  }

  public void setXmppRoster(IRoster paramIRoster)
  {
    this.mRoster = paramIRoster;
  }

  public void sortByDistance()
  {
    Collections.sort(this.mFriendList, new Comparator(this)
    {
      public int compare(, Friend paramFriend2)
      {
        int i;
        if (paramFriend1.getDistance() > paramFriend2.getDistance())
          i = 1;
        while (true)
        {
          while (true)
          {
            return i;
            if (paramFriend1.getDistance() != paramFriend2.getDistance())
              break;
            i = 0;
          }
          i = -1;
        }
      }
    });
  }

  public void sortFriendList()
  {
    Collections.sort(this.mFriendList, this.mComparator);
  }

  private class CallBack
  implements ProfileImageCacheCallback
  {
    public void refresh(, Bitmap paramBitmap)
    {
      this.this$0.notifyDataSetChanged();
    }
  }

  public class ComparatorContactListByStatusAndName<T>
  implements Comparator<T>
  {
    public int compare(, T paramT2)
    {
      int i;
      Friend localFriend1 = (Friend)paramT1;
      Friend localFriend2 = (Friend)paramT2;
      if ((localFriend1 == null) || (localFriend2 == null))
        i = 1;
      while (true)
      {
        return i;
        if (FriendAdapter.access$1(this.this$0) != null)
          break;
        i = localFriend1.getName().compareToIgnoreCase(localFriend2.getName());
      }
      Contact localContact1 = null;
      Object localObject = null;
      try
      {
        localContact1 = FriendAdapter.access$1(this.this$0).getContact(localFriend1.getID());
        Contact localContact2 = FriendAdapter.access$1(this.this$0).getContact(localFriend2.getID());
        localObject = localContact2;
        if ((localContact1 == null) || (localObject == null))
          i = 0;
      }
      catch (RemoteException localRemoteException)
      {
        while (true)
        {
          while (true)
          {
            localRemoteException.printStackTrace();
            if (localContact1.getStatus() >= localObject.getStatus())
              break;
            i = 1;
          }
          if (localContact1.getStatus() <= localObject.getStatus())
            break;
          i = -1;
        }
        i = localFriend1.getName().compareToIgnoreCase(localFriend2.getName());
      }
    }
  }

  private class ViewHoler
  {
    public TextView mDistance;
    public ImageView mIsFriend;
    public TextView mSignature;
    public FrameLayout mStatus;
    public ImageView mUserHead;
    public TextView mUserName;
  }
}